/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 */

package com.huawei.rpc.common.prorocol;

import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @Author Lijl
 * @ClassName HessianSerializeMessageProrocol
 * @Description Hessian 序列化信息
 * @Date 2022/2/17 15:16
 * @Version 1.0
 */
public class HessianSerializeMessageProrocol implements MessageProtocol{

    /**
     * @Author lijl
     * @MethodName serialize
     * @Description Hessian实现序列化
     * @Date 15:23 2022/2/17
     * @Version 1.0
     * @param object
     * @return: byte[]
    **/
    public byte[] serialize(Object object) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = null;
        HessianOutput hessianOutput = null;
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            // Hessian的序列化输出
            hessianOutput = new HessianOutput(byteArrayOutputStream);
            hessianOutput.writeObject(object);
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            throw e;
        } finally {
            try {
                if (byteArrayOutputStream!=null){
                    byteArrayOutputStream.close();
                }
            } catch (IOException e) {
                throw e;
            }
            try {
                if (hessianOutput!=null){
                    hessianOutput.close();
                }
            } catch (IOException e) {
                throw e;
            }
        }
    }

    /**
     * @Author lijl
     * @MethodName deserialize
     * @Description Hessian实现反序列化
     * @Date 15:29 2022/2/17
     * @Version 1.0
     * @param employeeArray
     * @return: java.lang.Object
    **/
    public Object deserialize(byte[] employeeArray) throws Exception {
        ByteArrayInputStream byteArrayInputStream = null;
        HessianInput hessianInput = null;
        try {
            byteArrayInputStream = new ByteArrayInputStream(employeeArray);
            // Hessian的反序列化读取对象
            hessianInput = new HessianInput(byteArrayInputStream);
            return hessianInput.readObject();
        } catch (IOException e) {
            throw e;
        } finally {
            try {
                if (byteArrayInputStream!=null){
                    byteArrayInputStream.close();
                }
            } catch (IOException e) {
                throw e;
            }
            try {
                if (hessianInput!=null){
                    hessianInput.close();
                }
            } catch (Exception e) {
                throw e;
            }
        }
    }

    @Override
    public byte[] marshallingRequest(Request request) throws Exception {
        return serialize(request);
    }

    @Override
    public Request unmarshallingRequest(byte[] data) throws Exception {
        return (Request) deserialize(data);
    }

    @Override
    public byte[] marshallingResponse(Response response) throws Exception {
        return serialize(response);
    }

    @Override
    public Response unmarshallingResponse(byte[] data) throws Exception {
        return (Response) deserialize(data);
    }
}
